package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AllProductRequest;
import com.mall.shopping.dto.AllProductResponse;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import com.mall.shopping.dto.GoodsResponse;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Hu
 * @create 2021-09-29 16:34
 * @Description
 */

@RestController
@RequestMapping("/shopping")
public class GoodsController {

    @Reference(timeout = 30000, retries = 0, check = false)
    IProductService iProductService;

    @RequestMapping("/goods")
    @Anoymous
    public ResponseData goodsList(HttpServletRequest request) {
        //获取request参数  request的参数需要校验么???
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String sort = request.getParameter("sort");
        String priceGt = request.getParameter("priceGt");
        String priceLte = request.getParameter("priceLte");
        String cid = request.getParameter("cid");

        AllProductRequest allProductRequest = new AllProductRequest();
        if (page == null) {
            page = "1";
        }
        allProductRequest.setPage(Integer.valueOf(page));
        allProductRequest.setSize(Integer.valueOf(size));
        allProductRequest.setSort(sort);
        if (priceGt != null && priceGt != "") {
            allProductRequest.setPriceGt(Integer.valueOf(priceGt));
        }
        if (priceLte != null && priceLte != "") {
            allProductRequest.setPriceLte(Integer.valueOf(priceLte));
        }
        if (cid != null&&cid!="") {
        allProductRequest.setCid(Long.valueOf(cid));
        }


        AllProductResponse allProduct = iProductService.getAllProduct(allProductRequest);
        GoodsResponse response = new GoodsResponse(allProduct.getProductDtoList(), allProduct.getTotal());
        response.setCode(allProduct.getCode());
        response.setMsg(allProduct.getMsg());

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());


    }

    @GetMapping("/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") long id){
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse productDetail = iProductService.getProductDetail(request);
        if (productDetail.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(productDetail.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }
}
